#!/bin/sh

# compares with lurette the results of:
#   lus2lic -2cmc
#   lus2lic -exec

lustre_file=$1
shift
OPT=$@

node=`basename $lustre_file .lus`
_oracle=_"$node"_oracle.lus
oracle="$node"_oracle
lv4="$node"_lv4.lus
ec="$node".ec
lv4_node="$node__$node"
env=_"$node"_env.lut
env_node="$node"_env


. $HOME/.opam/opam-init/variables.sh
set -x verbose #echo on


if
    [ -f $env_node ];
then
   echo "$env_node generated"

else if
     ./lus2lic $OPT $lustre_file -n $node --gen-autotest -np &&
     ./lus2lic $OPT $lustre_file -n $node -np -lic -o $lustre_file.lic &&
     cat $lustre_file.lic  >> $_oracle;
then
     echo "./lus2lic $OPT $lustre_file -n $node --gen-autotest : ok"
else
    echo "Error"
    exit 2
fi
fi

if
    ./lus2lic -dir /tmp/lustre-test-mt-$lustre_file-$node $OPT $_oracle -n $oracle  -2c -cc -o ${node}_oracle_mt.exec;
then
    echo "lus2lic -2c done"
else
    echo "Error"
    exit 2
fi

# generated the -2cmc version in ${node}_mt.exec
if
    ./lus2lic -dir /tmp/lustre-test-mt-$lustre_file-$node $OPT $lustre_file -n $node  -2cmtt &&
    ./lustre-mt /tmp/lustre-test-mt-$lustre_file-$node/${node}_${node}.yml &&
    mv wraptask.* /tmp/lustre-test-mt-$lustre_file-$node &&
    mv *env* /tmp/lustre-test-mt-$lustre_file-$node &&
    mv *oracle* /tmp/lustre-test-mt-$lustre_file-$node &&
    cd /tmp/lustre-test-mt-$lustre_file-$node/
    gcc -o ${node}_mt.exec ${node}_${node}.c ${node}_${node}_loop_io.c ${node}_${node}_loop.c lustre_consts.c wraptask.c -lpthread -lm
then
    echo "lus2lic -2cmt done"
else
    echo "Error"
    exit 2
fi

# call-via-socket -server -addr 127.0.0.1 -port $PORT ./${node}_mt.exec &

export PATH=/usr/local/tools/lustre/v4/bin/:$PATH
PRECISION=4
if
    lurette -l 10 -o xxx.rif  \
        -sut "./${node}_mt.exec" \
        -env "lutin $env -n $env_node -p $PRECISION -seed 42" \
        -oracle "./${node}_oracle_mt.exec";
# lurettetop_exe -p $PRECISION  -rp "sut:socket:127.0.0.1:$PORT"  -rp "env:lutin:$env:-p:$PRECISION"  -rp "oracle:v6:$_oracle:$oracle:$OPT" \
#    -go -l 10 -ns2c --stop-on-oracle-error;
then
    echo "Comparison of lus2lic -2c and lus2lic -exec with lurette on $lustre_file: ok"
    echo "lurette ok"
else
    echo "error"
     exit 2
fi
exit 0
